﻿using System;
using System.Windows.Forms;

namespace FileSynchronizer.Forms
{
    public partial class Form_NewFolderPair : Form
    {
        #region Private variables
        //---------------------------------------------------------------------------------------------------------------------------------------
        private string myLeftFolder;
        private string myRightFolder;
        private bool myAutoSynchronizeSetting;
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public variables and properties
        //---------------------------------------------------------------------------------------------------------------------------------------
        public string LeftFolder
        {
            get
            {
                return myLeftFolder;
            }
            set
            {
                myLeftFolder = value;
                textBox1.Text = myLeftFolder;
                CheckToEnableOKButton();
            }
        }

        public string RightFolder
        {
            get
            {
                return myRightFolder;
            }
            set
            {
                myRightFolder = value;
                textBox2.Text = myRightFolder;
                CheckToEnableOKButton();
            }
        }

        public bool AutoSynchronize
        {
            get
            {
                return myAutoSynchronizeSetting;
            }
            set
            {
                myAutoSynchronizeSetting = value;
                CheckAutoSynchronize.Checked = myAutoSynchronizeSetting;
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------
        public Form_NewFolderPair()
        {
            InitializeComponent();

            myLeftFolder = "";
            myRightFolder = "";

            LeftStatusLabel.Text = "";
            RightStatusLabel.Text = "";

            myAutoSynchronizeSetting = false;
            CheckAutoSynchronize.Checked = myAutoSynchronizeSetting;

            CheckToEnableOKButton();
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Event Handlers
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when the User clicks the "Browse" Button to choose a left folder
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void button_BrowseForSource_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = myLeftFolder;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                myLeftFolder = folderBrowserDialog1.SelectedPath;
                textBox1.Text = myLeftFolder;
            }
            CheckToEnableOKButton();
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when the User clicks the "Browse" Button to choose a right folder
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void button_BrowseForDestination_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = myRightFolder;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                myRightFolder = folderBrowserDialog1.SelectedPath;
                textBox2.Text = myRightFolder;
            }
            CheckToEnableOKButton();
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when the User clicks the "OK" Button to choose a right folder
        //---------------------------------------------------------------------------------------------------------------------------------------

        private void OnOKClick(object sender, EventArgs e)
        {
            myAutoSynchronizeSetting = CheckAutoSynchronize.Checked;
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Internal Methods
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Check if the new FolderPair is not included in previous FolderPairs
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void CheckToEnableOKButton()
        {
            if ((myLeftFolder != "") & (myRightFolder != ""))
            {
                if (myLeftFolder.StartsWith(myRightFolder))
                {
                    //
                    LeftStatusLabel.Text = "Folder is Subfolder of Right Folder !";
                    RightStatusLabel.Text = "";
                    button_OK.Enabled = false;
                }
                else
                {
                    if (myRightFolder.StartsWith(myLeftFolder))
                    {
                        //
                        LeftStatusLabel.Text = "";
                        RightStatusLabel.Text = "Folder is Subfolder of Left Folder !";
                        button_OK.Enabled = false;
                    }
                    else
                    {
                        LeftStatusLabel.Text = "";
                        RightStatusLabel.Text = "";

                        button_OK.Enabled = true;
                    }
                }
            }
            else
            {
                button_OK.Enabled = false;
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

    }
}
